layui.use(['form', 'table'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table;

    table.render({
        elem: '#currentTableId',
        url: '../Dealer/list',
        toolbar: '#toolbarDemo',
        defaultToolbar: ['filter', 'exports', 'print', {
            title: '提示',
            layEvent: 'LAYTABLE_TIPS',
            icon: 'layui-icon-tips'
        }],
        cols: [[
            {type: "checkbox", width: 50},
            { field: 'dealer_uid', width: 130, title: '经销商ID', sort: true , hide:true},
            {field: 'dealer', title: '经销商', sort: true},
            {field: 'shop', title: '店铺'},
            {field: 'ship_conditions', title: '发货条件'},
            {field: 'order_type', title: '订单类别'},

            {title: '操作', Width: 150, toolbar: '#currentTableBar', align: "center"}
        ]],
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true,
        skin: 'line'
        ,contentType: 'application/json'
        ,method:'Post'
    });

    // 监听搜索操作
    form.on('submit(data-search-btn)', function (data) {
        //执行搜索重载
        table.reload('currentTableId', {
            url: '../Dealer/list'
            , where : {
                dealer:data.field.dealer,
                shop:data.field.shop
            }
        },'data');
        // console.log("Debug res:",result);
        return false;
    });

    /**
     * toolbar监听事件
     */
    table.on('toolbar(currentTableFilter)', function (obj) {
        if (obj.event === 'add') {  // 监听添加操作
            var index = layer.open({
                title: '添加经销商',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: './Dealer-add.html',
                end: function(){
                    table.reload('currentTableId');
                },
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        } else if (obj.event === 'delete') {  // 监听删除操作
            var checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;
            layer.confirm("确认删除吗？",{
                title: '确认',
                end: function(){
                    table.reload('currentTableId');
                },
            },function(index){
                $.ajax({
                    url:"/Dealer/del",
                    type:"post",
                    data:JSON.stringify(data),
                    datatype:"json",
                });
                layer.close(index);
            });
        }
    });

    //监听表格复选框选择
    table.on('checkbox(currentTableFilter)', function (obj) {
        // console.log(obj)
    });

    table.on('tool(currentTableFilter)', function (obj) {
        var data = obj.data;
        if (obj.event === 'edit') {

            var index = layer.open({
                title: '编辑经销商',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '../../page/Dealer-edit.html?id=' + data.id,
                end: function(){
                    table.reload('currentTableId');
                },
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        } else if (obj.event === 'delete') {
            layer.confirm('真的删除行么', function (index) {
                obj.del();
                layer.close(index);
            });
        }
    });

});